<template><div><h2 id="运行测试" tabindex="-1"><a class="header-anchor" href="#运行测试"><span>运行测试</span></a></h2>
<p>Yii 中文文档 /</p>
<h2 id="测试" tabindex="-1"><a class="header-anchor" href="#测试"><span>测试</span></a></h2>
<p>Yii2 高级应用程序使用 Codeception 作为其主要测试框架。已经在 <code v-pre>frontend</code> ， <code v-pre>backend</code> 和 <code v-pre>common</code> 的 <code v-pre>tests</code> 目录中准备了一些样本测试。为了使以下过程工作，假定应用程序已使用初始化 <code v-pre>dev</code> 环境。 如果测试需要在 <code v-pre>Production</code> 环境中执行， <code v-pre>yii_test</code> 和 <code v-pre>yii_test.bat</code> 必须从 <code v-pre>environments/dev</code> 文件夹手动复制到项目根目录。测试需要一个 <strong>额外的数据库</strong> ，这将在测试之间清除。在 mysql 中创建数据库 <code v-pre>yii2advanced_test</code> （根据 <code v-pre>common/config/test.php</code> 中的配置）并执行：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./yii_test migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>构建测试套件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vendor/bin/codecept build</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后所有的样例测试可以通过运行如下代码：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vendor/bin/codecept run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您将看到类似于以下的输出：</p>
<p><img src="https://cdn.learnku.com/uploads/images/202201/27/93088/rWnN8jfRu6.png!large" alt="运行测试"></p>
<p>建议保持测试最新。 如果一个类或功能被删除，相应的测试也应该被删除。<br>
您应定期运行测试，或更好地为它们设置持续集成服务器。</p>
<p>请参考 <a href="http://codeception.com/for/yii" target="_blank" rel="noopener noreferrer">Codeception for Yii2 Framework Case Study</a> ，了解如何为应用程序配置代码。</p>
<h3 id="common" tabindex="-1"><a class="header-anchor" href="#common"><span>Common</span></a></h3>
<p>common部分的测试位于 <code v-pre>common/tests</code>. 在这个模板中只有 <code v-pre>unit</code> （单元）测试。<br>
运行如下代码:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vendor/bin/codecept run -- <span class="token parameter variable">-c</span> common</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>-c</code> 选项允许设置 <code v-pre>codeception.yml</code> 配置的路径。</p>
<p>在 <code v-pre>unit</code> 测试套件（位于 <code v-pre>common/tests/unit</code> ）中的测试可以使用Yii框架特性：<code v-pre>Yii::$app</code>，Active Record，fixtures等。这是因为 <code v-pre>Yii2</code> 模块在单元测试config：<code v-pre>common/tests/unit.suite.yml</code> 中被启用。 您可以禁用它以完全隔离的方式运行测试。</p>
<h3 id="frontend" tabindex="-1"><a class="header-anchor" href="#frontend"><span>Frontend</span></a></h3>
<p>前端测试包含单元测试，功能测试和验收测试。<br>
通过运行：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vendor/bin/codecept run -- <span class="token parameter variable">-c</span> frontend</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>测试套件描述：</p>
<ul>
<li><code v-pre>unit</code> ⇒ 仅与前端应用程序相关的类。</li>
<li><code v-pre>functional</code> ⇒ 应用程序内部请求/响应（无Web服务器）。</li>
<li><code v-pre>acceptance</code> ⇒ web应用程序，用户界面和javascript交互。</li>
</ul>
<p>默认情况下，验收测试被禁用，运行它们使用：</p>
<h4 id="运行验收测试" tabindex="-1"><a class="header-anchor" href="#运行验收测试"><span>运行验收测试</span></a></h4>
<p>要执行验收测试，请执行以下操作：</p>
<ol>
<li>
<p>重命名 <code v-pre>frontend/tests/acceptance.suite.yml.example</code> 为 <code v-pre>frontend/tests/acceptance.suite.yml</code> 以启用套件配置</p>
</li>
<li>
<p>在 <code v-pre>composer.json</code> 中替换 <code v-pre>codeception/base</code> 包为 <code v-pre>codeception/codeception</code> 以安装Codeception的全部功能</p>
</li>
<li>
<p>使用Composer更新依赖关系</p>
</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> update</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><ol start="4">
<li>为验收测试自动生成新的支持类:</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vendor/bin/codecept build -- <span class="token parameter variable">-c</span> frontend</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><ol start="5">
<li>下载 <a href="http://www.seleniumhq.org/download/" target="_blank" rel="noopener noreferrer">Selenium Server</a> 并启动:</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">java</span> <span class="token parameter variable">-jar</span> ~/selenium-server-standalone-x.xx.x.jar</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><ol start="6">
<li>启动web服务器:</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php <span class="token parameter variable">-S</span> <span class="token number">127.0</span>.0.1:8080 <span class="token parameter variable">-t</span> frontend/web</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><ol start="7">
<li>现在可以运行所有可用的测试</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vendor/bin/codecept run acceptance -- <span class="token parameter variable">-c</span> frontend</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="backend" tabindex="-1"><a class="header-anchor" href="#backend"><span>Backend</span></a></h2>
<p>后端应用程序包含单元和功能测试套件。 通过运行：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vendor/bin/codecept run -- <span class="token parameter variable">-c</span> backend</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


